package com.yzy.baselibrary.utils.permission;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;

/**
 * description: 各种rom的权限设置页面.
 *
 * @date 2019/1/5 15:07.
 * @author: YinZeYu.
 */
public class PermissionSettingPage {

  /**
   * 原生
   */
  public static Intent google(Context context) {
    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    intent.setData(Uri.fromParts("package", context.getPackageName(), null));
    return intent;
  }

  /**
   * 华为
   */
  public static Intent huawei(Context context) {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.huawei.systemmanager",
        "com.huawei.permissionmanager.ui.MainActivity"));
    if (hasIntent(context, intent)) return intent;
    intent.setComponent(new ComponentName("com.huawei.systemmanager",
        "com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity"));
    if (hasIntent(context, intent)) return intent;
    intent.setComponent(new ComponentName("com.huawei.systemmanager",
        "com.huawei.notificationmanager.ui.NotificationManagmentActivity"));
    return intent;
  }

  /**
   * 小米
   */
  public static Intent xiaomi(Context context) {
    Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
    intent.putExtra("extra_pkgname", context.getPackageName());
    if (hasIntent(context, intent)) return intent;

    intent.setPackage("com.miui.securitycenter");
    if (hasIntent(context, intent)) return intent;

    intent.setClassName("com.miui.securitycenter",
        "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
    if (hasIntent(context, intent)) return intent;

    intent.setClassName("com.miui.securitycenter",
        "com.miui.permcenter.permissions.PermissionsEditorActivity");
    return intent;
  }

  /**
   * oppo
   */
  public static Intent oppo(Context context) {
    Intent intent = new Intent();
    intent.putExtra("packageName", context.getPackageName());
    intent.setClassName("com.color.safecenter",
        "com.color.safecenter.permission.floatwindow.FloatWindowListActivity");
    if (hasIntent(context, intent)) return intent;

    intent.setClassName("com.coloros.safecenter",
        "com.coloros.safecenter.sysfloatwindow.FloatWindowListActivity");
    if (hasIntent(context, intent)) return intent;

    intent.setClassName("com.oppo.safe", "com.oppo.safe.permission.PermissionAppListActivity");
    return intent;
  }

  /**
   * vivo
   */
  public static Intent vivo(Context context) {
    Intent intent = new Intent();
    intent.setClassName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.FloatWindowManager");
    intent.putExtra("packagename", context.getPackageName());
    if (hasIntent(context, intent)) return intent;

    intent.setComponent(new ComponentName("com.iqoo.secure",
        "com.iqoo.secure.safeguard.SoftPermissionDetailActivity"));
    return intent;
  }

  /**
   * 魅族
   */
  public static Intent meizu(Context context) {
    Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
    intent.putExtra("packageName", context.getPackageName());
    intent.setComponent(
        new ComponentName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity"));
    return intent;
  }

  /**
   * 索尼
   */
  public static Intent sony(Context context) {
    Intent intent = new Intent();
    intent.putExtra("packageName", context.getPackageName());
    intent.setComponent(
        new ComponentName("com.sonymobile.cta", "com.sonymobile.cta.SomcCTAMainActivity"));
    return intent;
  }

  /**
   * 乐视
   */
  public static Intent leshi(Context context) {
    Intent intent = new Intent();
    intent.putExtra("packageName", context.getPackageName());
    intent.setComponent(
        new ComponentName("com.letv.android.letvsafe",
            "com.letv.android.letvsafe.PermissionAndApps"));
    return intent;
  }

  /**
   * LG
   */
  public static Intent lg(Context context) {
    Intent intent = new Intent("android.intent.action.MAIN");
    intent.putExtra("packageName", context.getPackageName());
    intent.setComponent(
        new ComponentName("com.android.settings",
            "com.android.settings.Settings$AccessLockSummaryActivity"));
    return intent;
  }

  /**
   * 判断要跳转的页面是否存在
   */
  public static boolean hasIntent(Context context, Intent intent) {
    return context.getPackageManager()
        .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
        .size() > 0;
  }
}
